########### Prepare the data for analysis ###################

# Clear memory

setwd("/Users/Neo/Desktop/replication_austerity")
set.seed(1234567)



# load packages
# install.packages(pacman)

library(pacman) 
pacman::p_load (ggplot2,foreign,lmtest,grid,sandwich,
                gridExtra,fpp,showtext,plyr,MASS,tseries,
                Rmisc,nlme,lme4,plm,arm,pcse,tseries,simcf,
                urca,Zelig,readstata13,foreign,lattice,effects,
                Hmisc,corrplot,interplot,reshape2,car,MASS,psych,
                GGally,splines,mgcv,sm,stargazer,AER,multilevel,
                lme4,lmerTest,ordinal,texreg,texreg,xtable,apsrtable,
                cowplot,nonnest2,dynsim,dynlm,TSA)

### Create dataset for analysis

data<- read.dta13('./data/austeritypaper_bjps.dta',convert.factors=T,convert.dates=T)

# Create lags
l.voteintentionpm_filtered<- lagpanel(data$voteintentionpm_filtered,data$countryid,data$trend,1)
l.voteintention_cabinet_filtered <- lagpanel(data$voteintention_cabinet_filtered,data$countryid,data$trend,1)
l.voteintentionfm_filtered <- lagpanel(data$voteintentionfm_filtered,data$countryid,data$trend,1)
l.vicab_raw_diff <- lagpanel(data$vicab_raw_diff,data$countryid,data$trend,1)
l.vipm_raw_diff <- lagpanel(data$vipm_raw_diff,data$countryid,data$trend,1)
l.vifm_raw_diff <- lagpanel(data$vifm_raw_diff,data$countryid,data$trend,1)
l.popw_econ_public <- lagpanel(data$popw_econ_public,data$countryid,data$trend,1)
l.dunemployment_yoy<- lagpanel(data$dunemployment_yoy,data$countryid,data$trend,1)
l.retail<- lagpanel(data$retail,data$countryid,data$trend,1)
l.retail_diff<- lagpanel(data$retail_diff,data$countryid,data$trend,1)
l.gdp_diff<- lagpanel(data$gdp_diff,data$countryid,data$trend,1)
l.austerity_pulse <- lagpanel(data$austerity_pulse,data$countryid,data$trend,1)


#### data with filtered cabinet series #####
# Listwise delete using only the data we need
cabinet_filter_data<- na.omit(cbind(
  data$country1,
  data$countryid,
  data$trend,

### cabinet vote intention series ###
  data$voteintention_cabinet_filtered,
  l.voteintention_cabinet_filtered,
  data$vi_csipolate,

### austerity index series ###
  data$austerity_12m,  
  data$austerity_6m, 
  data$austerity_3m, 
  

### contextual ###
  data$dunemployment_yoy,
  data$retail,
  l.retail,
  data$imf,
  data$popw_econ_public,  
  l.popw_econ_public,
  data$season,
  data$honeymoon,
  data$cabinetleft
))


cabinet_filter_data <- as.data.frame(cabinet_filter_data)

names(cabinet_filter_data) <- c("country",
                       "countryid",
                       "date_ym",
                       "vicab_filtered",
                       "l.vicab_filtered",
                       "vicab_raw",
                       "austerity_12m",    
                       "austerity_6m",  
                       "austerity_3m",  
                       "d.unemployment",
                       "retail",
                       "l.retail",
                       "imf",
                       "protest_freq",   
                       "l.protest_freq",
                       "season",
                       "honeymoon",
                       "cabinetleft")

lapply(cabinet_filter_data, class)
cabinet_filter_pdata <- pdata.frame(cabinet_filter_data, index=c("country", "date_ym"))



#### data with diffrenced cabinet series #####

# Listwise delete using only the data we need
cabinet_diff_data<- na.omit(cbind(
  data$country1,
  data$countryid,
  data$trend,
  
  ### vote intention series ###
  data$vicab_raw_diff,
  l.vicab_raw_diff,
  
  ### austerity index series ###
  data$austerity_12m,  
  data$austerity_6m, 
  data$austerity_3m, 
  
  ### contextual ###
  data$dunemployment_yoy,
  data$retail,
  l.retail,
  data$imf,
  data$popw_econ_public,  
  l.popw_econ_public,
  data$season,
  data$honeymoon
))


cabinet_diff_data <- as.data.frame(cabinet_diff_data)

names(cabinet_diff_data) <- c("country",
                              "countryid",
                              "date_ym",
                              "vicab_raw_diff",
                              "l.vicab_raw_diff",
                              "austerity_12m",    
                              "austerity_6m",  
                              "austerity_3m",  
                              "d.unemployment",
                              "retail",
                              "l.retail",
                              "imf",
                              "protest_freq",   
                              "l.protest_freq",
                              "season",
                              "honeymoon")

lapply(cabinet_diff_data, class)
cabinet_diff_pdata <- pdata.frame(cabinet_diff_data, index=c("country", "date_ym"))



#### data with filtered prime minister series #####
# Listwise delete using only the data we need
pm_filter_data<- na.omit(cbind(
  data$country1,
  data$countryid,
  data$trend,
  
  ### vote intention series ###
  data$voteintentionpm_filtered,
  l.voteintentionpm_filtered,
  data$vipm_csipolate,
  
  ### austerity index series ###
  data$austerity_12m,  
  data$austerity_6m, 
  data$austerity_3m, 
  
  ### contextual ###
  data$dunemployment_yoy,
  data$retail,
  l.retail,
  data$imf,
  data$popw_econ_public,  
  l.popw_econ_public,
  data$season,
  data$honeymoon
))


pm_filter_data <- as.data.frame(pm_filter_data)

names(pm_filter_data) <- c("country",
                           "countryid",
                           "date_ym",
                           "vipm_filtered",
                           "l.vipm_filtered",
                           "vipm_raw",
                           "austerity_12m",    
                           "austerity_6m",  
                           "austerity_3m",  
                           "d.unemployment",
                           "retail",
                           "l.retail",
                           "imf",
                           "protest_freq",   
                           "l.protest_freq",
                           "season",
                           "honeymoon")

lapply(pm_filter_data, class)
pm_filter_pdata <- pdata.frame(pm_filter_data, index=c("country", "date_ym"))


#### data with diffrenced prime minister series #####

# Listwise delete using only the data we need
pm_diff_data<- na.omit(cbind(
  data$country1,
  data$countryid,
  data$trend,
  
  ### vote intention series ###
  data$vipm_raw_diff,
  l.vipm_raw_diff,
  
  ### austerity index series ###
  data$austerity_12m,  
  data$austerity_6m, 
  data$austerity_3m, 
  
  ### contextual ###
  data$dunemployment_yoy,
  data$retail,
  l.retail,
  data$imf,
  data$popw_econ_public,  
  l.popw_econ_public,
  data$season,
  data$honeymoon
))


pm_diff_data <- as.data.frame(pm_diff_data)

names(pm_diff_data) <- c("country",
                         "countryid",
                         "date_ym",
                         "vipm_raw_diff",
                         "l.vipm_raw_diff",
                         "austerity_12m",    
                         "austerity_6m",  
                         "austerity_3m",  
                         "d.unemployment",
                         "retail",
                         "l.retail",
                         "imf",
                         "protest_freq",   
                         "l.protest_freq",
                         "season",
                         "honeymoon")

lapply(pm_diff_data, class)
pm_diff_pdata <- pdata.frame(pm_diff_data, index=c("country", "date_ym"))



#### data with filtered finance minister series #####
# Listwise delete using only the data we need
fm_filter_data<- na.omit(cbind(
  data$country1,
  data$countryid,
  data$trend,
  
  ### vote intention series ###
  data$voteintentionfm_filtered,
  l.voteintentionfm_filtered,
  data$vifm_csipolate,
  
  ### austerity index series ###
  data$austerity_12m,  
  data$austerity_6m, 
  data$austerity_3m, 
  
  ### contextual ###
  data$dunemployment_yoy,
  data$retail,
  l.retail,
  data$imf,
  data$popw_econ_public,  
  l.popw_econ_public,
  data$season,
  data$honeymoon
))


fm_filter_data <- as.data.frame(fm_filter_data)

names(fm_filter_data) <- c("country",
                           "countryid",
                           "date_ym",
                           "vifm_filtered",
                           "l.vifm_filtered",
                           "vifm_raw",
                           "austerity_12m",    
                           "austerity_6m",  
                           "austerity_3m",  
                           "d.unemployment",
                           "retail",
                           "l.retail",
                           "imf",
                           "protest_freq",   
                           "l.protest_freq",
                           "season",
                           "honeymoon")

lapply(fm_filter_data, class)
fm_filter_pdata <- pdata.frame(fm_filter_data, index=c("country", "date_ym"))


#### data with diffrenced finance minister series #####

# Listwise delete using only the data we need
fm_diff_data<- na.omit(cbind(
  data$country1,
  data$countryid,
  data$trend,
  
  ### vote intention series ###
  data$vifm_raw_diff,
  l.vifm_raw_diff,
  
  ### austerity index series ###
  data$austerity_12m,  
  data$austerity_6m, 
  data$austerity_3m, 
  
  ### contextual ###
  data$dunemployment_yoy,
  data$retail,
  l.retail,
  data$imf,
  data$popw_econ_public,  
  l.popw_econ_public,
  data$season,
  data$honeymoon
))


fm_diff_data <- as.data.frame(fm_diff_data)

names(fm_diff_data) <- c("country",
                         "countryid",
                         "date_ym",
                         "vifm_raw_diff",
                         "l.vifm_raw_diff",
                         "austerity_12m",    
                         "austerity_6m",  
                         "austerity_3m",  
                         "d.unemployment",
                         "retail",
                         "l.retail",
                         "imf",
                         "protest_freq",   
                         "l.protest_freq",
                         "season",
                         "honeymoon")

lapply(fm_diff_data, class)
fm_diff_pdata <- pdata.frame(fm_diff_data, index=c("country", "date_ym"))



#### data with filtered cabinet series and austerity pulse #####
# Listwise delete using only the data we need
cabinet_filter_pulse_data<- na.omit(cbind(
  data$country1,
  data$countryid,
  data$trend,
  
  ### vote intention series ###
  data$voteintention_cabinet_filtered,
  l.voteintention_cabinet_filtered,
  data$vi_csipolate,
  
  ### austerity index series ###
  data$austerity_pulse,
  l.austerity_pulse,
  
  ### contextual ###
  data$dunemployment_yoy,
  data$retail,
  l.retail,
  data$imf,
  data$popw_econ_public,  
  l.popw_econ_public,
  data$season,
  data$honeymoon
))


cabinet_filter_pulse_data <- as.data.frame(cabinet_filter_pulse_data)

names(cabinet_filter_pulse_data) <- c("country",
                                      "countryid",
                                      "date_ym",
                                      "vicab_filtered",
                                      "l.vicab_filtered",
                                      "vicab_raw",
                                      "austerity_pulse", 
                                      "l.austerity_pulse",
                                      "d.unemployment",
                                      "retail",
                                      "l.retail",
                                      "imf",
                                      "protest_freq",   
                                      "l.protest_freq",
                                      "season",
                                      "honeymoon")

lapply(cabinet_filter_pulse_data, class)
cabinet_filter_pulse_pdata <- pdata.frame(cabinet_filter_pulse_data, index=c("country", "date_ym"))


rm(l.voteintentionpm_filtered,
   l.voteintention_cabinet_filtered,
   l.voteintentionfm_filtered,
   l.vicab_raw_diff,
   l.vipm_raw_diff,
   l.vifm_raw_diff,
   l.popw_econ_public,
   l.dunemployment_yoy,
   l.retail,
   l.retail_diff,
   l.gdp_diff,
   l.austerity_pulse)


#### data for Greece MITS #####

data<- read.dta13('./data/mits_greece.dta',convert.factors=T,convert.dates=T)

greece <- na.omit(cbind(
  data$trend1,
  data$year,
  data$month,
  data$voteintention_cabinet_filtered,
  data$austerity_12m,  
  data$austerity_6m, 
  data$austerity_3m, 
  data$firstbailout_step,
  data$midterm_step,
  data$secondbailout_step,
  data$thirdbailout_level
))
greece <- as.data.frame(greece)
names(greece) <- c("t",
                   "year",
                   "month",
                   "voteintention_cabinet",
                   "austerity_12m",    
                   "austerity_6m",  
                   "austerity_3m",  
                   "first_bailout",
                   "mid_term",
                   "second_bailout",
                   "third_bailout")

greece <- ts(greece, start=c(2007,1), frequency=12)